package managerEngine

import (
	"net/http"
	"net/http/cookiejar"

	"github.com/go-resty/resty/v2"
	"golang.org/x/net/publicsuffix"
)

// ManagerEngineCookies 卓豪的爬虫接口定义，主要是获取数据
// Cookies 登录过程，返回登陆后的Cookies
type ManagerEngineSpider interface {
	// Cookies 为登录
	Login(username, password string) bool
	// CheckCookieUsefull() bool
	// SetCookies(u *url.URL, cookies []*http.Cookie)
	// UpdateCookie 更新全局Spider的Cookies
	// UpdateCookies(cookies []*http.Cookie)
	SerializerCookies(filePath string, cookie []*http.Cookie) error
	UnSerializerCookies(filePath string) ([]*http.Cookie, error)
	GetNetWorkData(url string, params *map[string]string) (*resty.Response, error)
	// PostData(url string, params map[string]interface{})
}

// NewReportParamas 生成对应的参数
type OpManagerQueryParama interface {
	CPUQueryParam(ip string) *map[string]string
	DiskQueryParam(ip string) []*map[string]string
	MemQueryParam(ip string) *map[string]string
	ProcessQueryParam(ip string) map[string]*map[string]string
	ServiceQueryParam(ip string) []*map[string]string
	DeviceSummaryQueryParam(ip string) *map[string]string
}

var (
	Spider *resty.Client
	Jar    *cookiejar.Jar
)

// init 初始化一个包内全局Spider的http client。
func init() {
	Jar, _ = cookiejar.New(&cookiejar.Options{
		PublicSuffixList: publicsuffix.List,
	})

	Spider = resty.NewWithClient(
		&http.Client{
			// CheckRedirect: func(req *http.Request, via []*http.Request) error {
			// 	return http.ErrUseLastResponse
			// 	// return nil nil回重定向。
			// },
			Jar: Jar,
		},
	)
}
